package cn.tedu.tea.service;

import cn.tedu.tea.domain.Order;
import cn.tedu.tea.domain.param.OrderAddNewDTO;
import cn.tedu.tea.domain.param.OrderUpdateDTO;
import cn.tedu.tea.domain.vo.OrderListVO;
import cn.tedu.tea.domain.vo.OrderStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.OrderMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void addNew(OrderAddNewDTO orderAddNewDTO) {
        log.debug("开始处理【添加订单】的业务，参数： {}", orderAddNewDTO);
//        int count = orderMapper.countByOrderNo(orderAddNewDTO.getOrderNo());
//        if (count > 0){
//            String message = "添加订单失败，系统文件名已被占用！";
//            log.warn(message);
//            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
//        }
        Order order = new Order();
        BeanUtils.copyProperties(orderAddNewDTO, order);
        order.setModifiedUserId(0).setModifiedUserName("");
        int rows = orderMapper.insert(order);
        if (rows != 1){
            String message = "添加订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除订单】的业务，参数： {}", id);
        OrderStandardVO orderStandard = orderMapper.getStandardById(id);
        if (orderStandard == null){
            String message = "删除订单失败，要删除的订单不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = orderMapper.deleteById(id);
        if (rows != 1){
            String message = "删除订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除订单】的业务，参数： {}", ids);
        int rows = orderMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(OrderUpdateDTO orderUpdateDTO) {
        log.debug("开始处理【修改订单信息】的业务，参数：{}", orderUpdateDTO);
        OrderStandardVO orderStandard = orderMapper.getStandardById(orderUpdateDTO.getId());
        if (orderStandard == null){
            String message = "修改订单失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = orderMapper.countByOrderNoAndNotId(orderUpdateDTO.getOrderNo(), orderUpdateDTO.getId());
        if (count > 0){
            String message = "修改订单失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Order order = new Order();
        BeanUtils.copyProperties(orderUpdateDTO, order);
        int rows = orderMapper.update(order);
        if (rows != 1){
            String message = "修改订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public OrderStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询订单详情】的业务，参数： {}", id);
        OrderStandardVO orderStandard = orderMapper.getStandardById(id);
        if (orderStandard == null){
            String message = "查询订单详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return orderStandard;
    }

    @Override
    public List<OrderListVO> list() {
        log.debug("开始处理【查询订单列表】的业务，无参数");
        List<OrderListVO> list = orderMapper.list();
        return list;
    }

}
